﻿Imports System.Data.SqlServerCe
Imports Framework.Data

Public Class DownloadsFileSubscriptionsEx
    Shared Sub New()
        Dim aSelectColumns As String() = {"Id", "BuddyId", "SHA512Hash", "Size"}
        ObjectPersister.DefineObject(GetType(Entities.Downloads.FileSubscription), "Downloads_FileSubscriptions", aSelectColumns)
    End Sub
    Private Sub New()
    End Sub

    Public Shared Function GetAll() As Generic.List(Of Entities.Downloads.FileSubscription)
        Return ObjectPersister.GetAll(Of Entities.Downloads.FileSubscription)()
    End Function
    Public Shared Function GetBySHA512HashAndSize(ByVal sha512Hash As String, ByVal size As Long) As Generic.List(Of Entities.Downloads.FileSubscription)
        Dim strClause As String = "[SHA512Hash]=@Hash AND [Size]=@Size"
        Dim sqlPar1 As New SqlCeParameter("@Hash", sha512Hash)
        Dim sqlPar2 As New SqlCeParameter("@Size", size)
        Return ObjectPersister.GetAll(Of Entities.Downloads.FileSubscription)(strClause, sqlPar1, sqlPar2)
    End Function

    Public Shared Sub Save(ByVal subscription As Entities.Downloads.FileSubscription, ByVal transactionScope As TransactionScope)
        Dim clsCmd As New SaveCommand(Of Integer)("Downloads_FileSubscriptions", "Id", subscription.Id, 0)
        clsCmd.AddParam("SHA512Hash", subscription.SHA512Hash, False)
        clsCmd.AddParam("Size", subscription.Size, False)
        clsCmd.AddParam("BuddyId", subscription.BuddyId, False)

        subscription.Id = clsCmd.Save(transactionScope)
    End Sub

    Public Shared Sub Delete(ByVal id As Integer, ByVal transactionScope As TransactionScope)
        Dim strSql As String = "DELETE FROM Downloads_FileSubscriptions WHERE [Id]=@Id"
        Dim sqlCmd As Command = Environment.NewCommand(strSql, transactionScope)
        sqlCmd.AddParameterWithValue("@Id", id)
        Environment.ExecuteNonQuery(sqlCmd)
    End Sub
    Public Shared Sub DeleteAllByBuddyId(ByVal buddyId As Integer, ByVal transactionScope As TransactionScope)
        Dim strSql As String = "DELETE FROM Downloads_FileSubscriptions WHERE [BuddyId]=@BuddyId"
        Dim sqlCmd As Command = Environment.NewCommand(strSql, transactionScope)
        sqlCmd.AddParameterWithValue("@BuddyId", buddyId)
        Environment.ExecuteNonQuery(sqlCmd)
    End Sub
End Class
